(function (window) {
    var Subject_offline_select_courseCtrl = {
        data: {},
        init: function (data) {
            if (view_type != "Web") {
                $("#offline_select").attr("h", $(window).height() - 150 - 120)
            }
            this.bindEven();
            Subject_offline_select_courseCtrl.data = data;
            $("#select_day_div .wday").eq(0).trigger(hiApp.device.click);

            $("#offline_select").on("swiperSlideInit", function (e, s) {
                Subject_offline_select_courseCtrl.bindEven();
                $("#select_day_div .wday").eq(0).trigger(hiApp.device.click);

            })

            $("#offline_select").on("swiperSlideInitPage", function (e, s) {
                Subject_offline_select_courseCtrl.bindEven();
                $("#select_day_div .wday").eq(0).trigger(hiApp.device.click);

            })


        },
        afterShow: function () {

        },

        bindEven: function () {
            var bindings = [{
                // element:document,
                element: '#select_day_div .wday',
                event: hiApp.device.click,
                handler: this.select_class
                },
                {
                    // element:document,
                    element: '#class_room_id',
                    event: "change",
                    handler: this.select_timeQuantum
                },
                {
                    // element:document,
                    element: '#part_times .wday',
                    event: hiApp.device.click,
                    handler: this.spanactive
                },
                {
                    // element:document,
                    element: '#offline_select_seat',
                    event: "click",
                    handler: this.select_seat_1
                },
                {
                    // element:document,
                    element: '#classrooms_seat .og-li a',
                    event: hiApp.device.click,
                    handler: this.select_seat
                }, {
                    // element:document,
                    element: '#book_now',
                    event: hiApp.device.click,
                    handler: this.book_now
                }

            ];
            bindEvents(bindings);
        },

        select_class: function () {
            $("#select_day_div").find(".wday").removeClass("spanactive");
            $(this).addClass("spanactive");
            var week = $(this).attr("week");
            var class_room = $("#class_room_id_html").find('[data-week="' + week + '"]');
            $("#class_room_id").find("option").remove();
            $("#class_room_id").val(0);
            if (class_room.length > 0) {
                $("#class_room_div").show();
                class_room.clone().appendTo( $("#class_room_id"));
                $("#class_room_id").find("option").eq(0).attr("selected", "selected");
            }else{
                $("#class_room_div").hide();
            }
            Subject_offline_select_courseCtrl.select_timeQuantum();
        },

        select_timeQuantum: function () {
            var class_room_id = $("#class_room_id").val();
            var week = $("#select_day_div .spanactive").attr("week");
            $("#part_times").find(".wday").removeClass("spanactive");
            $("#part_times").find(".wday").hide();
            var timeQuantum = $("#time_quantum_div").find('[data-classrooms="' + week + "_" + class_room_id + '"]');
            //	alert('[data-classrooms="'+week+"_"+class_room_id+'"]');
            if (timeQuantum.length > 0) {
                timeQuantum.show();
                timeQuantum.eq(0).addClass("spanactive");
                $("#time_quantum_div").show();
            } else {
                $("#time_quantum_div").hide();
            }

        },
        spanactive: function () {
            $(this).parent().find(".wday").removeClass("spanactive");
            $(this).addClass("spanactive");
        },
        select_seat_1: function () {
            if ($("#part_times").find(".spanactive").length == 0) {
                dialog("请选择时间段");
                return;
            }
            var id = $("#part_times").find(".spanactive").attr("data-id");
            var limit_seat = $("#part_times").find(".spanactive").attr("limit_seat");
            if (limit_seat == 0) {
                dialog("当前时间段的座位已经抢光");
            } else {
                var subjectid = Subject_offline_select_courseCtrl.data.query.subjectid;
                hiApp.jump(base_url + "/Subject/offline_select_seat/id/" + id + "/subjectid/" + subjectid);
            }
        },
        select_seat: function () {
            var class_name = $(this).attr("class");
            if (class_name == "dis") {
                return false;
            }
            var un_choose_img = $("#un_choose_img").attr("src");
            var choose_img = $("#choose_img").attr("src");
            $("#classrooms_seat .og-li").find("[choose='1']").find("img").attr("src", un_choose_img);
            $("#classrooms_seat .og-li").find("[choose='1']").attr("choose", "0");
            if (class_name != "dis") {
                $(this).attr("choose", "1");
                $(this).find("img").attr("src", choose_img);
            }
        },
        book_now: function () {
            var seat_num = $("#classrooms_seat .og-li").find("[choose='1']").attr("num");
            //	console.log(Subject_offline_select_courseCtrl.data);
            var part_time_id = Subject_offline_select_courseCtrl.data.query.id;
            var subjectid = Subject_offline_select_courseCtrl.data.query.subjectid;
            $.getJSON("/Schools/act_book", {seat_num: seat_num, part_time_id: part_time_id}, function (data) {
                if (data.error > 0) {
                    dialog(data.msg);
                } else {
                    dialog(data.msg, function () {
                        hiApp.jump("/Subject/offline_select_record/subjectid/" + subjectid + "/time/" + Date.parse(new Date()));
                    }, 1000);
                }
            })
        }

    }
    window.BoxEdu_Subject_offline_select_courseCtrl = Subject_offline_select_courseCtrl;

})(window);